package cn.itcast.web.controller.email.listener;

import cn.itcast.domain.system.User;
import cn.itcast.web.controller.BaseController;
import cn.itcast.web.utils.MailUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;

public class EmailListener extends BaseController implements MessageListener {

    private ObjectMapper objectMapper = new ObjectMapper();

    @Override
    public void onMessage(Message message) {

        try {
            //1. 获取消息
            byte[] body = message.getBody();
            //2.转换为User
            User user = objectMapper.readValue(body, User.class);
            //3. 发送邮件
            /*
            sendMsg(String to, String subject, String content)
                    to: 接收人
                    subject：主题
                    content:邮件正文
             */
            String subject = "购销合同的交货时间";
            MailUtil.sendMsg(user.getEmail(), subject, user.getUserName()+",你好！\n 你签订合同的交货时间快要到了，还有3天,请尽快交货!");
            System.out.println("邮件发送完毕..");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
